package cn.javayong.aviatorscript.demo;


import cn.javayong.aviatorscript.demo.function.AddFunction;
import com.alibaba.fastjson2.JSONObject;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AviatorDemoUnitTest {


    /**
     * 算数表达式
     */
    @Test
    public void test1() {
        // 返回值为 18
        Long r = (Long) AviatorEvaluator.execute("2 * (4 + 5)");
        System.out.println(r);
    }

    @Test
    public void test2() {
        Expression expression = AviatorEvaluator.compile("2 * (4 + 5)");
        Long r = (Long) expression.execute();
        // 返回值为 18
        System.out.println(r);
    }

    @Test
    public void test3() {

        // 返回 hello world
        String r = (String) AviatorEvaluator.execute("'hello' + ' world'");
        System.out.println(r);

        // 返回 true
        Boolean r2 = (Boolean) AviatorEvaluator.execute("100 > 80 && 30 < 40");
        System.out.println(r2);

        // 三元表达式，返回 30
        Long r3 = (Long) AviatorEvaluator.execute("100 > 80 ? 30 : 40");
        System.out.println(r3);

        // 正则表达式，正则表达式放在//之间，返回 true
        Boolean r4 = (Boolean) AviatorEvaluator.execute("'hello' =~ /[\\w]+/");
        System.out.println(r4);

    }

    //  表达式变量
    @Test
    public void test4() {
        Long a = 12L;
        Boolean r = (Boolean) AviatorEvaluator.exec("a > 10", a);
        System.out.println(r);

        // 列表对象
        List<Long> b = new ArrayList<>();
        b.add(12L);
        b.add(20L);
        Boolean r2 = (Boolean) AviatorEvaluator.exec("b[0] > 10", b);
        System.out.println(r2);

        // 普通对象
        Person c = new Person("movee", 25);
        Boolean r3 = (Boolean) AviatorEvaluator.exec("c.age > 10", c);
        System.out.println(r3);

        // map 对象
        Map<String, Object> env = new HashMap<>();
        env.put("person", new Person("movee", 25));
        env.put("a", 20L);
        Object result = AviatorEvaluator.execute("person.name", env);
        System.out.println(result);
    }

    // 测试基本函数
    @Test
    public void test5() {
        // 返回4
        Long r = (Long) AviatorEvaluator.execute("math.round(4.3)");

        // 返回5
        Long r1 = (Long) AviatorEvaluator.execute("string.length('hello')");

        // 返回一个ArrayList：[1,2,3]
        Object r2 = AviatorEvaluator.execute("seq.list(1,2,3)");

    }

    // 自定义函数
    @Test
    public void test6() {
        // 注册
        AviatorEvaluator.addFunction(new AddFunction());
        // 使用
        long sum = (Long) AviatorEvaluator.getInstance().execute("add(3,4)");
        System.out.println(sum);
    }

    public static class Person {

        private String name;
        private Integer age;

        // 带参构造方法
        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        // Getter 和 Setter 方法
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        // 可选：重写 toString 方法，方便打印对象信息
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

}
